package ahblue.cn.tanzi.ui.person;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.adapter.BRAdapter;
import ahblue.cn.tanzi.adapter.RvHolder;
import ahblue.cn.tanzi.model.collect.CollectModel;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.ui.circle.CircleDetailActivity;
import ahblue.cn.tanzi.ui.home.HomeFragment;
import ahblue.cn.tanzi.ui.home.article.ArticleDetailActivity;
import ahblue.cn.tanzi.ui.home.video.VideoDetailActivity;
import ahblue.cn.tanzi.util.ImageLoadUtil;
import ahblue.cn.tanzi.util.TimeUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author pink-jackstraw
 * @date 2018/11/10
 */
public class CollectActivity extends BaseActivity {

    @BindView(R.id.recy)
    RecyclerView recyview;
    @BindView(R.id.refresh)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.empty)
    RelativeLayout empty;
    private BRAdapter<CollectModel> adapter;
    private List<CollectModel> dataList = new ArrayList<>();
    private int page = 0;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    swipeRefreshLayout.setRefreshing(false);
//                    adapter.notifyDataSetChanged();
                    //swipeRefreshLayout.setEnabled(false);
                    break;
                default:
                    break;
            }
        }
    };

    public static void into(Activity activity) {
        activity.startActivity(new Intent(activity, CollectActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        CollectModel.igetCollect(this, LoginResultModel.getToken(this), new CollectModel.CollectListener() {
            @Override
            public void conver(List<CollectModel> model) {
                if (null != model) {
                    dataList.addAll(model);
                }

                if (dataList.size() == 0) {
                    empty.setVisibility(View.VISIBLE);
                    swipeRefreshLayout.setVisibility(View.GONE);
                } else {
                    empty.setVisibility(View.GONE);
                    swipeRefreshLayout.setVisibility(View.VISIBLE);
                }
            }
        });
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        recyview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BRAdapter<CollectModel>(this, R.layout.activity_collect_item, dataList) {
            @Override
            protected void convert(RvHolder holder, CollectModel CollectModel, int position) {
                //holder.setText(R.id.name, CollectModel.member_name + " " + CollectModel.comment_count + "评论");
                holder.setText(R.id.name, CollectModel.member_name);
                holder.setText(R.id.time, TimeUtils.getStandardDate(CollectModel.add_time));
                holder.setText(R.id.sign, CollectModel.title);
                ImageLoadUtil.load(CollectActivity.this, CollectModel.image, holder.getView(R.id.img));
                ImageLoadUtil.load(CollectActivity.this, CollectModel.member_avatar, holder.getView(R.id.icon));
                holder.setOnLongClickListener(R.id.root, new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        showMDialog(position);
                        return true;
                    }
                });
                holder.setOnClickListener(R.id.root, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (CollectModel.type == 2) {
                            VideoDetailActivity.into(CollectActivity.this, CollectModel.id);
                        } else if (CollectModel.type == 1) {
                            final Intent intent = new Intent(CollectActivity.this, ArticleDetailActivity.class);
                            intent.putExtra(ArticleDetailActivity.ARG_ARTICLE_ID, Integer.parseInt(CollectModel.id));
                            intent.putExtra(ArticleDetailActivity.ARG_ARTICLE_TITLE, CollectModel.title);
                            startActivity(intent);

                        }
                    }
                });
            }
        };
        recyview.setAdapter(adapter);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
//        swipeRefreshLayout.setProgressBackgroundColor(R.color.colorAccent);
        //swipeRefreshLayout.setPadding(20, 20, 20, 20);
        //swipeRefreshLayout.setProgressViewOffset(true, 100, 200);
        //swipeRefreshLayout.setDistanceToTriggerSync(50);
        swipeRefreshLayout.setProgressViewEndTarget(true, 100);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
//                        dataList.clear();
//                        page = 0;
//                        initData();
                        mHandler.sendEmptyMessage(1);
                    }
                }).start();
            }
        });
    }

    public void showMDialog(int position) {
        AlertDialog alertDialog = new AlertDialog.Builder(CollectActivity.this)
                .setItems(new String[]{"删除"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CollectModel.delCollect(CollectActivity.this, LoginResultModel.getToken(CollectActivity.this),
                                dataList.get(position).id,
                                dataList.get(position).type + "",
                                new CollectModel.DelCollectListener() {
                                    @Override
                                    public void conver(CollectModel model) {
                                        if (null != model) {
                                            dataList.remove(position);
                                            adapter.notifyDataSetChanged();
                                        }
                                    }
                                });
                    }
                }).create();
        alertDialog.show();
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
